// pages/address/address.js
var app = getApp();
import { getAddress, delAddress, defAddress } from '../../config/https';
import Dialog from '../../vant/dialog/dialog';
import Notify from '../../vant/notify/notify';
import Toast from '../../vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
  },

  init() {
    Toast.loading({
      mask: true,
      message: '加载中...'
    });
    getAddress({
      user_id: app.globalData.user_id
    }).then(res => {
      this.setData(res);
      Toast.clear();
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //前往编辑地址页面
  editAddress(e) {
    const info = e.currentTarget.dataset.info;
    const data = {
      username: info.shperson || '', //收货人姓名
      mobile: info.mobile || '', //收货人手机号
      address: info.region || '', //收货人地址
      detail: info.detail_info || '', //详细信息
      address_id: info.id || '',
      latitude: info.latitude || '',
      longitude: info.longitude || '',
      checked: info.status == 1 ? true : false
    }
    wx.navigateTo({
      url: `../addressInfo/addressInfo?info=${JSON.stringify(data)}`,
    })
  },

  //删除收货地址
  deleteAddress(e) {
    if (e.currentTarget.dataset.status == 1){
      this.failNot('默认地址不能删除！')
    }else{
      Dialog.confirm({
        message: '确定删除该地址吗？'
      }).then(() => {
        const address_id = e.currentTarget.dataset.id;
        delAddress({
          user_id: app.globalData.user_id,
          address_id
        }).then(res => {
          this.succNot('删除成功')
          if (this.data.result.length > 0) {
            //获取页面栈
            let pages = getCurrentPages();
            //获取上一个页面
            let prev = pages[pages.length - 2];
            //调用上一个页面的setData方法，从而达到naviagtreBack返回传参的效果
            prev.setData({ address: this.data.result[0] });
          }
          setTimeout(() => { this.init() }, 1000)
        }).catch(err => {
          this.failNot(err || '删除地址失败！')
        });
      })
    }
  },

  setDefault(e){
    const address_id = e.currentTarget.dataset.id;
    defAddress({
      user_id: app.globalData.user_id,
      address_id
    }).then(res => {
      this.init()
    }).catch(err => {
      this.failNot(err || '设置默认地址失败！')
    })
  },

  selectAddress(e){
    if (this.options.select){
      const address = e.currentTarget.dataset.info;
      //获取页面栈
      let pages = getCurrentPages();
      //获取上一个页面
      let prev = pages[pages.length - 2];
      //调用上一个页面的setData方法，从而达到naviagtreBack返回传参的效果
      prev.setData({ address });
      wx.navigateBack();
    }
  },

  succNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#1989fa'
    });
  },

  failNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#f44'
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.init()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})